var Geolocation = (function () {
    var autocomplete, input;
    function initialize(inputId, callback) {
        input = document.getElementById(inputId);
        autocomplete = new google.maps.places.Autocomplete(
            (document.getElementById(inputId)),
            {types: ['geocode']}
        );
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            callback(autocomplete);
        });
        if (window.addEventListener) {
            input.addEventListener('focus', geolocate, false);
        } else if (window.attachEvent) {
            input.attachEvent('onfocus', geolocate);
        } else {
            input['onfocus'] =  geolocate;
        }
    }
    function geolocate() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var geolocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                autocomplete.setBounds(new google.maps.LatLngBounds(geolocation, geolocation));
            });
        }
    }
    return {
        initialize: function(inputId, callback){
            initialize(inputId, callback);
        }
    }
})();

$(document).on('click', '.save-data-button', function () {
    $(this).siblings('.save-data-form').toggle(); 
});
$('body').on('focus', "#nombreForm, #emialForm", function () {
    $(this).next().html('')
})
$('body').on('click', '#guardarEmail', function (event) {
    event.preventDefault();
    var $form = $(this).parents('form'), data = $form.serialize(), error=false,
        emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    
    if (!emailRegex.test($form.find('#emialForm').val())) {
        $('label[for="emialForm"]').html('El email ingresado no es v&aacute;lido');
        error = true;
    }
    if (!$form.find('#nombreForm').val()) {
        $('label[for="nombreForm"]').html('El campo nombre es obligatorio');
        error = true;
    }
    if (!$form.find('#emialForm').val()) {
        $('label[for="emialForm"]').html('El campo email es obligatorio');
        error = true;
    }
    
    if (!error) {
        $form.find('input[type="text"]').attr('disabled', true);
        $(this).find('.fa-circle-o-notch').show();
        $.ajax({
            url: BASE_URL+'save-email',
            type:'post',
            'data': data,
            success: function () {
                //successBox
                $('#successBox .success-box-message').html('El mail fue enviado exitosamente a su casiila de correo');
                $('#successBox').fadeIn(600, function (){
                    setTimeout(function () {
                        $('#successBox').fadeOut(600)
                    }, 1800)
                })
                
                //fadeout y reset del form
                $form.find('.fa-circle-o-notch').hide();
                $form.find('.reponse').show();
                $form.find('#guardarEmail').hide();
                setTimeout(function () {
                    $form.parents('.save-data-form').fadeOut(function () {
                        $form.find('input[type="text"]').each(function () {
                        $(this).attr('disabled', false).val('');
                    })
                    $form.find('.reponse').hide();
                    $form.find('#guardarEmail').show();
                    });
                }, 600)
            }
        })
    }
})